"use server"
import { redirect } from "next/navigation"
import { cookies }from"next/headers" 
import { revalidatePath } from "next/cache"
import axios from "axios";

const url="http://127.0.0.1:8000/api/yjstudent/add"
const baseUrl="http://127.0.0.1:8000/api/yjstudent/"


 const fetchUsers = async (q) => {
    console.log("进入fetchUsers,q是:" + q);
    const response = await axios(baseUrl, {
        method: 'GET',
        params: {
            searchName: q
        }
    });
    const students = response.data;
    console.log("fetchUsers返回的数据是:" + students);
    return students;
};
// try{
//     const cookie=cookies().get("Authrization")
//     if(cookie){
//         const token=cookie.value
//         console.log("token"+token)
//     }
// }catch(err){
//     console.log(err)
// }


export const addStudent=async(prevState,fromData)=>{
    console.log("进入Studentaction")
    const id=fromData.get("id")
    const name=fromData.get("name")
    console.log("id:"+id,"name:"+name)

    const res=await fetch(url,{
        method:"POST",
        headers:{
            "Content-Type":"application/json",
            // "Authorization":`Bearer ${cookies().get("Authrization").value}`
        },
        body:JSON.stringify({id,name})
    })
    const json=await res.json()

    if(res.ok){
        console.log("添加成功")
        revalidatePath("/studentManage")
        redirect("/studentManage")
        
    }else{
        return json.error
    }
}


export const deleteStudent=async(id,fromData)=>{
    const res=await fetch(`http://127.0.0.1:8000/api/yjstudent/${id}`,{
        method:"DELETE",
        headers:{
            "Content-Type":"application/json",
            // "Authorization":`Bearer ${cookies().get("Authrization").value}`
        },
    });
   
    const json=await res.json()
    
    
    if(res.ok){
        console.log("删除成功")

        revalidatePath("/studentManage")
        redirect("/studentManage")
        
    }else{
        return json.error
    }
}

export const fetchById=async(id)=>{
    const res=await fetch(`http://127.0.0.1:8000/api/yjstudent/${id}`,{
        method:"GET",
        headers:{
            "Content-Type":"application/json",
            // "Authorization":`Bearer ${cookies().get("Authrization").value}`
        },
    });
   
    const student=await res.json()
    return student
    
   
}

export const updateStudent=async(fromData)=>{
    console.log("进入Studentaction")
    const id=fromData.get("id")
    const name=fromData.get("name")
    console.log("id:"+id,"name:"+name)

    const res=await fetch(`http://127.0.0.1:8000/api/yjstudent/${id}`,{
        method:"PUT",
        headers:{
            "Content-Type":"application/json",
            // "Authorization":`Bearer ${cookies().get("Authrization").value}`
        },
        body:JSON.stringify({id,name})
    })
    const json=await res.json()

    if(res.ok){
        console.log("修改成功")
        // revalidatePath("/bookmarks")
        redirect("/studentManage")
        
    }else{
        return json.error
    }
}
export default fetchUsers

